#ifndef YHAIDA_HTTPREQUEST_H
#define YHAIDA_HTTPREQUEST_H

#include "../../server/datetime/Timestamp.h"

#include <map>
#include <assert.h>
#include <stdio.h>

namespace yhaida
{
    /*
        ---- 请求行 ----
            GET /url HTTP/1.1  \r\n # GET请求方式 请求资源路径 HTTP协议版本
        ---- 请求头 -----
            Host: www.itcast.cn  \r\n # 服务器的主机地址和端口号,默认是80
            Connection: keep-alive \r\n # 和服务端保持长连接
            Upgrade-Insecure-Requests: 1 \r\n # 让浏览器升级不安全请求，使用https请求
            User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36  \r\n # 用户代理，也就是客户端的名称
            Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp # 可接受的数据类型
            Accept-Encoding: gzip, deflate \r\n # 可接受的压缩格式
            Accept-Language: zh-CN,zh;q=0.9 \r\n #可接受的语言
            Cookie: pgv_pvi=1246921728; \r\n # 登录用户的身份标识
        ---- 空行 ----
            //...
    */
    class HttpRequest
    {
    public:
        enum Method
        {
            kInvalid,
            kGet,
            kPost,
            kHead,
            kPut,
            kDelete
        };
        enum Version
        {
            kUnknown,
            kHttp10,
            kHttp11
        };
    public:
        HttpRequest();
        ~HttpRequest() = default;

        void setVersion(Version v);
        Version getVersion() const;

        bool setMethod(const char *start, const char *end);
        Method method() const;
        const char *methodString() const;

        void setPath(const char *start, const char *end);
        const std::string &path() const;
        void setQuery(const char *start, const char *end);
        const std::string &query() const;

        void setReceiveTime(Timestamp t);
        Timestamp receiveTime() const;
        void addHeader(const char *start, const char *colon, const char *end);
        std::string getHeader(const std::string &field) const;
        const std::map<std::string, std::string> &headers() const;

        void setBody(std::string body);
        const std::string getBody() const;


        void swap(HttpRequest &that);

        


    private:
        Method _method;
        Version _version;
        std::string _path;
        std::string _query;
        Timestamp _receiveTime;
        std::map<std::string, std::string> _headers;
        std::string _body;
    };
}

#endif